package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.util.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;
/** 
 * Action: Checks the named Group to see if it has been scheduled and completed.
 * We need some extra fields in the various Groups to indicate their done status.
 * The start and end times denote the period over which we are checking. 
 * Strictly we need to return a history over this period rather than a simple
 * true/false and date.
 * $Id: CHECK_GROUPImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class CHECK_GROUPImpl extends TransactionImpl implements Logging {
    
    /** The path used to identify the Group in the OSS database. */
    protected Path groupPath;

    /** The start of the time period to check whether Group was done.*/
    protected long startTime;

    /** The end of the time period to check whether Group was done.*/    
    protected long endTime;


    public CHECK_GROUPImpl(CHECK_GROUP request) {
	super(request);
	this.groupPath = request.getGroupPath();
	this.startTime = request.getStartTime();
	this.endTime   = request.getEndTime();
	myLog = LogManager.getLogger("TRACE");
    }

    /**
     * Checks the done state of the named Group.
     * @return
     */
    public TRANSACTION_DONE exec() {
		
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    
	    myLog.log(1, CLASS, id, "exec", "Checking Group: "+groupPath.toString());	    
	    myLog.log(1, CLASS, id, "exec", "Searching for owner Proposal:");
	    
	    Proposal proposal = null;
	    try {
		proposal = OSS_Utilities.fetchProposal(groupPath);
	    } catch (IOException iox) {
		myLog.log(1, CLASS, id, "exec",
			   "Proposal: "+groupPath.getProposalByName()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
			     "Proposal: "+groupPath.getProposalByName()+
			     " Error reading from database",
			     iox);
	    }		
	    
	    // Check it exists.
	    if (proposal == null) {
		myLog.log(1, CLASS, id, "exec",
			   "Proposal: "+groupPath.getProposalByName()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Proposal: "+groupPath.getProposalByName());
	    } 
	    
	    myLog.log(1, CLASS, id, "exec", "Fetched Proposal: "+proposal.getName());
	    
	    Group group = proposal.findGroup(groupPath.getGroupByName());
	    
	    // Check it exists.
	    if (group == null) {
		myLog.log(1, CLASS, id, "exec",
			   "Group: "+groupPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Group: "+groupPath.toString());
	    }
	    
	    myLog.log(1, CLASS, id, "exec", "Found Group:");
	    
	    // We need the Group's done status here and some way to return it.
	    
	    List history = group.getHistory();
	    myLog.log(1, CLASS, id, "exec", "Group history "+
		       (history != null ? "contain "+history.size()+" entries" : "is NULL"));
	    // we could either return the list or prune it between the specified dates.
	    
	    CHECK_GROUP_DONE done = new CHECK_GROUP_DONE(id); 
	    done.setSuccessful(true);
	    done.setHistory(history);
	    return done;	  
	    
	} finally {
	    t.commit();
	    myLog.log(1, CLASS, id, "exec", logBuffer.toString());
	}
    }
}

/** $Log: CHECK_GROUPImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2006/11/27 08:37:05  snf
/** Initial revision
/** */
